<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/WEB-INF/view/jsp/public/include.base.jsp" %>
<html>
<head>
    <title>地图打点</title>
    <jsp:include page="/WEB-INF/view/jsp/public/include.inc.top.jsp" flush="true"/>
    <style type="text/css">
        .setting-location {
            height: 400px;
            margin-top: 10px;
        }
    </style>
</head>
<body>
<div class="input-group">
    <input id="txtAddress" class="form-control" type="text" value="${param.address}"/>
    <span class="btn btn-success input-group-addon" onclick="getLocation()">定位地址</span>
</div>
<div id="location-map" class="form-group setting-location"></div>
</body>
<jsp:include page="/WEB-INF/view/jsp/public/include.inc.bottom.jsp" flush="true"/>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=${ak}"></script>
<script type="text/javascript">
    var ready = false;
    var map = new BMap.Map('location-map');
    var geo = new BMap.Geocoder();
    var marker, address, city, lng, lat;
    map.addEventListener('load', function () {
        ready = true;
        var _lng = getQueryString('lng');
        var _lat = getQueryString('lat');
        var _address = getQueryString('address');
        if (_lng != '' && _lat != '' && _address != '') {
            if (marker != null)
                map.removeOverlay(marker);
            var p = new BMap.Point(_lng, _lat);
            marker = new BMap.Marker(p);
            map.addOverlay(marker);
            map.panTo(p);
            document.getElementById('txtAddress').value = _address;
            address = _address;
        }
    });

    var getQueryString = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return decodeURIComponent(r[2]);
        return null;
    };

    var _city = getQueryString('city');
    if (_city != '' && _city != null)
        map.centerAndZoom(_city, 13);
    else
        map.centerAndZoom('长沙市', 13);
    map.enableScrollWheelZoom();
    map.addEventListener('click', function (e) {
        if (marker != null)
            map.removeOverlay(marker);
        marker = new BMap.Marker(e.point);
        map.addOverlay(marker);
        geo.getLocation(e.point, function (a) {
            if (a) {
                if (a.addressComponents != null && a.addressComponents != '')
                    city = a.addressComponents.city;
                if (a.address == '') {
                    if (marker != null)
                        map.removeOverlay(marker);
                    address = '';
                    lng = null;
                    lat = null;
                    document.getElementById('txtAddress').value = '';
                    return;
                }
                address = a.address;

                document.getElementById('txtAddress').value = a.address;
            }
        });
        lng = e.point.lng;
        lat = e.point.lat;
    });
    var getLocation = function () {
        if (!ready) {
            return;
        }
        var val = $.trim(document.getElementById('txtAddress').value);
        if (val.length == 0) {
            window.top.bootbox.alert({
                title: '<span style="font-size: 14px;"><B>提示</B></span>',
                message: "<div style='text-align: left;'><span style='font-size: 14px;'>地址不能为空</span></div>",
                className: "bootbox-sm"
            });
            return;
        }
        geo.getPoint(val, function (point) {
            if (point) {
                geo.getLocation(point, function (a) {
                    if (a && a.addressComponents != null && a.addressComponents != '') {
                        city = a.addressComponents.city;
                    }
                });
                if (marker != null)
                    map.removeOverlay(marker);
                marker = new BMap.Marker(point);
                map.addOverlay(marker);
                map.panTo(point);
                address = val;
                lng = point.lng;
                lat = point.lat;
            } else {
                window.top.bootbox.alert({
                    title: '<span style="font-size: 14px;"><B>提示</B></span>',
                    message: "<div style='text-align: left;'><span style='font-size: 14px;'>无法定位该地址</span></div>",
                    className: "bootbox-sm"
                });
            }
        });
    };
    $('#txtAddress').bind('keyup', function (e) {
        if (e.keyCode == 13)
            getLocation();
    });
</script>
</html>
